import { ref, reactive } from 'vue'
import { useTransition } from '@vueuse/core'
import { listRiskRecordApi, statisticRiskRecordApi } from '../api/riskRecord'

const riskRecordListLoading = ref(true)
const riskRecordList = reactive({
  arr: []
})
const riskRecordSearchForm = reactive({})
const currentPage = ref(1)
const pageSize = ref(9)
const total = ref(0)
const dayRiskRecordNum = ref(0)
const weekRiskRecordNum = ref(0)
const dayRiskRecordNumT = useTransition(dayRiskRecordNum, { duration: 1000 })
const weekRiskRecordNumT = useTransition(weekRiskRecordNum, { duration: 1000 })
useTransition(dayRiskRecordNum, { duration: 1000 })
useTransition(weekRiskRecordNum, { duration: 1000 })
async function getRiskRecordList() {
  riskRecordListLoading.value = true
  const { data: res } = await listRiskRecordApi(
    riskRecordSearchForm,
    currentPage.value,
    pageSize.value
  )
  riskRecordList.arr = res.data.records
  total.value = res.data.total
  setTimeout(() => {
    riskRecordListLoading.value = false
  }, 500)
}
async function getStatisticRiskRecord() {
  const { data: res } = await statisticRiskRecordApi()
  dayRiskRecordNum.value = res.data.dayRiskRecordNum
  weekRiskRecordNum.value = res.data.weekRiskRecordNum
}

export const useRiskRecord = () => {
  return {
    riskRecordListLoading,
    currentPage,
    pageSize,
    total,
    riskRecordList,
    riskRecordSearchForm,
    dayRiskRecordNumT,
    weekRiskRecordNumT,
    getRiskRecordList,
    getStatisticRiskRecord
  }
}
